const { ordersModel } = require('./models/ordersModel')
const { parkingsModel } = require('./models/parkingsModel')
const { tenantsModel } = require('./models/tenantsModel')
const { lessorsModel } = require('./models/lessorsModel')

var mongoose = require('mongoose')
/*1.管理员 */
//获取订单列表
module.exports.getOrders = async function (orders) {
    const total = await ordersModel.countDocuments();
    const ordersData = await ordersModel.find().populate({
        path: "parkingsId",
        populate: {
            path: "lessorsId"
        }
    }).populate({
        path: 'tenantsId'
    }).limit(orders.pageSize - 0).skip((orders.currentPage - 1) * orders.pageSize)
    return { total, ordersData }
}


//查询订单
module.exports.searchOrders = async function (data) {
    const tenants = await tenantsModel.find({
        name: { $regex: data.searchData.name }
    })
    const parkings = await parkingsModel.find({
        address: { $regex: data.searchData.address }
    })
    const ordersData = await ordersModel.find({
        $and: [
            { state: data.searchData.searchType },
            { startTime: { $regex: data.searchData.startTime } },
            { tenantsId: { $in: tenants } },
            { parkingsId: { $in: parkings } }
        ]
    }).populate({
        path: "parkingsId",
        populate: {
            path: "lessorsId"
        }
    }).populate({
        path: 'tenantsId'
    }).limit(data.pageSize - 0).skip((data.currentPage - 1) * data.pageSize)
    return { total: ordersData.length, ordersData }
}


//删除
module.exports.deleteOrders = async function (_id) {
    return await ordersModel.updateOne({ _id }, { delete: true })

}


/*出租方 */
//获取订单列表
module.exports.getOrdersById = async function (data) {
    //出租方对应的所有车位
    const parkings = await parkingsModel.find({
        $and: [
            { lessorsId: mongoose.Types.ObjectId(data.searchData.lessorsId) },
        ]
    })
    const ordersData = await ordersModel.find({
        $and: [
            { state: data.searchData.searchType },
            { parkingsId: { $in: parkings } }
        ]
    }).populate({
        path: "parkingsId",
        populate: {
            path: "lessorsId"
        }
    }).populate({
        path: 'tenantsId'
    }).limit(data.pageSize - 0).skip((data.currentPage - 1) * data.pageSize)
    return { total: ordersData.length, ordersData }
}


//查找列表，根据出租方id查找列表
module.exports.searchOrdersById = async function (data) {
    //出租方对应的所有车位
    const parkings = await parkingsModel.find({
        $and: [
            { lessorsId: mongoose.Types.ObjectId(data.searchData.lessorsId) },
            { address: { $regex: data.searchData.address } }
        ]
    })
    const tenants = await tenantsModel.find({
        name: { $regex: data.searchData.name }
    })
    const ordersData = await ordersModel.find({
        $and: [
            { state: data.searchData.searchType },
            { startTime: { $regex: data.searchData.startTime } },
            { tenantsId: { $in: tenants } },
            { parkingsId: { $in: parkings } }
        ]
    }).populate({
        path: "parkingsId",
        populate: {
            path: "lessorsId"
        }
    }).populate({
        path: 'tenantsId'
    }).limit(data.pageSize - 0).skip((data.currentPage - 1) * data.pageSize)

    return { total: ordersData.length, ordersData }
}


//删除
module.exports.deleteOrdersById = async function (_id) {
    return await ordersModel.updateOne({ _id }, { delete: true })

}


// 引入时间插件
const moment = require('moment');
//订单生成
module.exports.addOrder = async function (code) {
    return await ordersModel.create({ parkingsId: code.parkingId, tenantsId: code.tenants, state: 2, startTime: moment().format('YYYY-MM-DD HH:mm') })
}



//通过租客id查找订单
module.exports.searchById = async function (id) {
    const data = await ordersModel.find({ tenantsId: id.id }).populate({
        path: 'tenantsId',
    }).populate({
        path: 'parkingsId',
        populate: {
            path: 'lessorsId'
        }
    })
    return data
}


//修改状态（还车位）
module.exports.changeOrdersState = async function ({ id }) {
    const data = await ordersModel.updateOne({ _id: id }, { state: 4, endTime: moment().format('YYYY-MM-DD HH:mm') })
    return data
}